#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#define high 480
#define width 640
#define ballnum 15
 
int main()
{
    float ball_x[ballnum],ball_y[ballnum];
    float ball_vx[ballnum],ball_vy[ballnum];
    float radius;
    int i,j;
    radius=20;

    for(i=0;i<ballnum;i++)
    {
        ball_x[i]=rand()%int (width-4*radius)+2*radius;
        ball_y[i]=rand()%int (high-4*radius)+2*radius;
        ball_vx[i]=(rand()%2)*2-1;
        ball_vy[i]=(rand()%2)*2-1;
        
    }

    initgraph(width,high);
    BeginBatchDraw();
    while(1)
    {
        setcolor(BLACK);
        setfillcolor(BLACK);
        for(i=0;i<ballnum;i++)
        {
            fillcircle(ball_x[i],ball_y[i],radius);
        }
        for(i=0;i<ballnum;i++)
        {
            ball_x[i]=ball_x[i]+ball_vx[i];
            ball_y[i]=ball_y[i]+ball_vy[i];
            
            if(ball_x[i]<radius)
                ball_x[i]=radius;
            if(ball_y[i]<radius)
                ball_y[i]=radius;
            if(ball_x[i]>width-radius)
                ball_x[i]=width-radius;
            if(ball_y[i]>high-radius)
                ball_y[i]=high-radius;
        }
        
        //判断是否和墙壁碰撞
        for(i=0;i<ballnum;i++)
        {
            if((ball_x[i]<=radius)||(ball_x[i]>=width-radius))
                ball_vx[i]=-ball_vx[i];
            if((ball_y[i]<=radius)||(ball_y[i]>=high-radius))
                ball_vy[i]=-ball_vy[i];
        }
        float mindistance2[ballnum][2]; //记录某个小球和与它最近小球的距离，以及这个小球的下标
        for(i=0;i<ballnum;i++)
        {
            mindistance2[i][0]=999999;
            mindistance2[i][1]=-1;
        }
        //求所有小球两两之间的距离的平方
        for(i=0;i<ballnum;i++)
        {
            for(j=0;j<ballnum;j++)
            {
                if(i!=j) //不需要和自己比
                {
                    float dist2;
                    dist2=(ball_x[i]-ball_x[j])*(ball_x[i]-ball_x[j])+(ball_y[i]-ball_y[j])*(ball_y[i]-ball_y[j]);
                    if(dist2<mindistance2[i][0])
                    {
                        mindistance2[i][0]=dist2;
                        mindistance2[i][1]=j;
                    }
                }
            }
        }
        //判断球之间是否碰撞
        for(i=0;i<ballnum;i++)
        {
            if(mindistance2[i][0]<=4*radius*radius)//若最小距离小于阀值，发生碰撞
            {
                j=mindistance2[i][1];
                int temp;
                temp=ball_vx[i];ball_vx[i]=ball_vx[j];ball_vx[j]=temp;
                temp=ball_vy[i];ball_vy[i]=ball_vy[j];ball_vy[j]=temp;
                
                mindistance2[j][0]=9999999; //避免交换两次速度
                mindistance2[j][1]=-1;
            }
        }
        
        setcolor(YELLOW);
        setfillcolor(GREEN);
        for(i=0;i<ballnum;i++)
            fillcircle(ball_x[i],ball_y[i],radius);
        FlushBatchDraw();
        
        Sleep(3);
    }
    EndBatchDraw();
    closegraph();
    return 0;
}